package com.bykj.property.api.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bykj.property.api.service.WxAppUserInfoService;
import com.bykj.property.api.util.HttpUtils;
import com.bykj.property.api.vo.PmProprietorVo;
import com.bykj.property.mapper.AppPropOpenMapper;
import com.bykj.property.mapper.PmProprietorMapper;
import com.bykj.property.model.AppPropOpen;
import com.bykj.property.model.PmProprietor;
import com.bykj.property.model.PmUnit;
import com.bykj.property.model.PmVillage;
import com.bykj.property.service.PmUnitService;
import com.bykj.property.service.PmVillageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WxAppUserInfoServiceImpl implements WxAppUserInfoService {
    private static final String APPID = "wxf265b49346c0c5f0";// 微信应用唯一标识
    private static final String SECRET = "253438120935785f2bfc1609b2255cd5";

    @Autowired
    private AppPropOpenMapper appPropOpenMapper;
    @Autowired
    private PmVillageService pmVillageService;
    @Autowired
    private PmUnitService pmUnitService;
    @Autowired
    private PmProprietorMapper pmProprietorMapper;
    @Override
    public String getWxUserOpenid(@NotEmpty String code) {

        JSONObject jsonObject = code2sessionKey(code);
        String openId = jsonObject.getString("openid");
        log.info("openId======" + openId);
        return openId;
    }

    @Override
    public String accountUserIsbind(@NotEmpty String openid) {
        AppPropOpen appPropOpen = appPropOpenMapper.selectById(openid);
        if (appPropOpen != null)
            return appPropOpen.getIsbind();
        return "0";
    }

    @Override
    public List<PmVillage> accountUserSelectOptionBuildings() {
        return pmVillageService.list();
    }

    @Override
    public List<PmUnit> accountUserSelectOptionUnits(@NotEmpty String pmVillageId) {
        List<PmUnit> allPmUnits = pmUnitService.list();
        List<PmUnit> pmUnits = new ArrayList<>();
        if (allPmUnits != null && allPmUnits.size() != 0) {
            for (PmUnit pmUnit : allPmUnits) {
                if (pmUnit != null && pmUnit.getPmVillageId().toString().equals(pmVillageId)) {
                    pmUnits.add(pmUnit);
                }
            }
        }
        return pmUnits;
    }

    @Override
    public int accountUser(@NotEmpty String building, @NotEmpty String unit, @NotEmpty String room, @NotEmpty String username, @NotEmpty String role, @NotEmpty String phone, @NotEmpty String openid) {
        PmProprietor pmProprietor = new PmProprietor();
        pmProprietor.setPmVillageId(Long.valueOf(building));
        pmProprietor.setPmUnitId(Long.valueOf(unit));
        pmProprietor.setDoorNumber(room);
        pmProprietor.setPropName(username);
        pmProprietor.setPhone(phone);
        pmProprietor.setIsHolder(role);
        QueryWrapper<PmProprietor> pmProprietorWrapper = new QueryWrapper<>();
        pmProprietorWrapper.setEntity(pmProprietor);
        PmProprietor pmProprietorDB = pmProprietorMapper.selectOne(pmProprietorWrapper);
        int i = 0;
        if (pmProprietorDB!=null){
            AppPropOpen appPropOpen = new AppPropOpen();
            appPropOpen.setOpenid(openid);
            appPropOpen.setIsbind("1");
            appPropOpen.setPmProprietorId(pmProprietorDB.getPmProprietorId());
            i = appPropOpenMapper.updateById(appPropOpen);
        }
        return i;
    }

    @Override
    public PmProprietorVo accountgetUser(@NotEmpty String openid) {
        AppPropOpen appPropOpen = new AppPropOpen();
        appPropOpen.setOpenid(openid);
        QueryWrapper<AppPropOpen> appPropOpenQueryWrapper = new QueryWrapper<>();
        appPropOpenQueryWrapper.setEntity(appPropOpen);
        AppPropOpen appPropOpenDB = appPropOpenMapper.selectOne(appPropOpenQueryWrapper);
        if (appPropOpenDB.getPmProprietorId()!=null){

            PmProprietor pmProprietor = pmProprietorMapper.selectById(appPropOpenDB.getPmProprietorId());
            PmUnit pmUnit = pmUnitService.getById(pmProprietor.getPmUnitId());
            PmVillage pmVillage = pmVillageService.getById(pmProprietor.getPmProprietorId());
            PmProprietorVo pmProprietorVo = JSON.parseObject(JSON.toJSONString(pmProprietor),PmProprietorVo.class);
            pmProprietorVo.setBuilding(pmVillage.getViName());
            pmProprietorVo.setUnit(pmUnit.getUnName());
            return pmProprietorVo;
        }else{
            return null;
        }
    }


    private JSONObject code2sessionKey(@NotEmpty String code) {
        /*String stringToken = String.format(
                "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
                APPID, SECRET, code);*/
        String stringToken = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+code+"&grant_type=authorization_code";
        String response = HttpUtils.httpsRequestToString(stringToken, "GET", null);
        return JSON.parseObject(response);
    }
}
